﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using vdos.jobseeker.Utils;

namespace vdos.jobseeker.AdminCP.Job
{
    public partial class job_edit : System.Web.UI.Page
    {
        protected Int64 jobID;
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (Session["USER"] == null) { Response.Redirect("/login.aspx"); }
            //else
            //{
            //    if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "SYSADMIN") == false)
            //    {
            //        if (hash1way.CheckPermission(hash1way.GetPermission(Session["USER"].ToString()), "HOMEPAGE") == false)
            //        {
            //            Response.Redirect("/message_box.aspx?why=1&msg=5");
            //        }
            //    }
            //}

            if (!IsPostBack)
            {
                jobID = ConvertUtility.ToInt64(Request.QueryString["jobID"]);

                if (jobID > 0)
                {
                    DataTable dt;

                    dt = DataHelper.SelectTable("SELECT CompanyID, CompanyName FROM Company ORDER BY CompanyName", false);
                    CompanyID.DataSource = dt;
                    CompanyID.DataTextField = "CompanyName";
                    CompanyID.DataValueField = "CompanyID";
                    CompanyID.DataBind();

                    dt = DataHelper.SelectTable("SELECT CityID, CityName FROM Cities ORDER BY CityName", false);
                    JobCities.DataSource = dt;
                    JobCities.DataTextField = "CityName";
                    JobCities.DataValueField = "CityID";
                    JobCities.DataBind();

                    dt = DataHelper.SelectTable("SELECT IndustryID, IndustryName FROM Industrial ORDER BY IndustryName", false);
                    JobIndustries.DataSource = dt;
                    JobIndustries.DataTextField = "IndustryName";
                    JobIndustries.DataValueField = "IndustryID";
                    JobIndustries.DataBind();

                    dt = DataHelper.SelectTable("SELECT JobLevelID, JobLevelName FROM JobLevel ORDER BY JobLevelName", false);
                    JobLevel.DataSource = dt;
                    JobLevel.DataTextField = "JobLevelName";
                    JobLevel.DataValueField = "JobLevelID";
                    JobLevel.DataBind();
                    JobPostDate.Value = DateTime.Today.ToString("dd/MM/yyyy");


                    dt = DataHelper.SelectTable("SELECT * FROM Jobs", false);
                    if (dt.Rows.Count > 0)
                    {
                        CompanyID.SelectedValue = dt.Rows[0]["CompanyID"].ToString();
                        JobPosition.Value = dt.Rows[0]["JobPosition"].ToString();
                        JobDescription.Value = dt.Rows[0]["JobDescription"].ToString();
                        JobRequire.Value = dt.Rows[0]["JobRequire"].ToString();
                        JobLevel.SelectedValue = dt.Rows[0]["JobLevelID"].ToString();
                        JobSalaryLevel.Value = dt.Rows[0]["JobSalaryLevel"].ToString();
                        JobType.Value = dt.Rows[0]["JobType"].ToString();
                        JobPostDate.Value = Convert.ToDateTime(dt.Rows[0]["JobPostDate"]).ToString("dd/MM/yyyyy");
                        JobDeadlineToApply.Value = Convert.ToDateTime(dt.Rows[0]["JobDeadlineToApply"]).ToString("dd/MM/yyyyy");
                        JobHot.Checked = Convert.ToBoolean(dt.Rows[0]["JobHot"]);

                    }
                    dt = DataHelper.SelectTable("Select CityID from dbo.Job_Cities where JobID = " + jobID + "", false);
                    string sArrayCityID ="";
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (i != dt.Rows.Count - 1) { sArrayCityID += dt.Rows[i]["CityID"].ToString() +","; }
                        else { sArrayCityID += dt.Rows[i]["CityID"].ToString(); }
                        aJobCities.Value = sArrayCityID;  
                    }
                    dt = DataHelper.SelectTable("Select IndustryID from dbo.Job_Industrial where JobID = " + jobID + "", false);
                    string sArrayIndustryID = "";
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (i != dt.Rows.Count - 1) { sArrayIndustryID += dt.Rows[i]["IndustryID"].ToString() + ","; }
                        else { sArrayIndustryID += dt.Rows[i]["IndustryID"].ToString(); }
                        aJobIndustries.Value = sArrayIndustryID;
                    }

                }
            }

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            update();
            Response.Redirect("/Job/job_all_published.aspx");
        }

        protected void update()
        {
            try
            {
                //jobID = ConvertUtility.ToInt64(Request.QueryString["jobID"]);
                //var item = new clsJobs();
                //item.CompanyID = Convert.ToInt32(CompanyID.SelectedValue);
                //item.JobPosition = JobPosition.Value;
                //item.JobDescription = JobDescription.Value;
                //item.JobRequire = JobRequire.Value;
                //item.JobLevelID = Convert.ToInt32(JobLevel.SelectedValue);
                //item.JobSalaryLevel = JobSalaryLevel.Value;
                //item.JobType = JobType.Value;
                //item.JobPostDate = Convert.ToDateTime(Utility.SwitchDateTime(JobPostDate.Value));
                //if (JobHot.Checked) item.JobHot = 1;
                //else item.JobHot = 0;
                //item.JobDeadlineToApply = Convert.ToDateTime(Utility.SwitchDateTime(JobDeadlineToApply.Value));
                //item.JobID = jobID;
                //item.Update();
                
                //DataHelper.ExecuteNonQuery("Delete Job_Cities where JobID = "+jobID,true);
                //DataHelper.ExecuteNonQuery("Delete Job_Industrial where JobID = " + jobID, true);

                //var cityItem = aJobCities.Value.Split(',');
                //for (int i = 0; i < cityItem.Count(); i++)
                //{
                //    DataHelper.ExecuteNonQuery("Insert Job_Cities values(" + jobID + ", " + cityItem[i] + ")",true);
                //}
                //var industryItem = aJobIndustries.Value.Split(',');
                //for (int i = 0; i < industryItem.Count(); i++)
                //{
                //    DataHelper.ExecuteNonQuery("Insert Job_Industrial values(" + jobID + ", " + industryItem[i] + ")", true);
                //}
            }
            catch (Exception ex)
            {

            }
        }

        protected Int64 getUserID()
        {

            DataTable dt = DataHelper.SelectTable("SELECT UserID FROM [User] WHERE Username ='" + Session["USER"].ToString() + "'", false);
            if (dt.Rows.Count > 0)
            {
                return ConvertUtility.ToInt64(dt.Rows[0]["UserID"]);
            }
            else
            {
                return 0;
            }
        }
    }
}
